package com.easyCode.mysql.mock;

import com.easyCode.dataMock.MockSqlParse;
import com.easyCode.dataMock.TableMockInfo;
import com.easyCode.sqlBuilder.sqlUtil.SqlBuildUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * @author 大忽悠
 * @create 2023/1/18 13:25
 */
@Slf4j
public class MysqlMockSqlParse implements MockSqlParse {
    @Override
    public String mockSql(String tableName, List<TableMockInfo.ColumnHolder> columnHolderList) {
        if(columnHolderList.isEmpty()){
            return "";
        }
        StringBuilder sqlBuilder=new StringBuilder();
        sqlBuilder.append("insert into ").append(tableName).append("(").append(SqlBuildUtil.wrapSqlParam(columnHolderList.get(0).getColumnName()));
        columnHolderList.stream().skip(1).forEach(columnHolder -> {
            sqlBuilder.append(",").append(SqlBuildUtil.wrapSqlParam(columnHolder.getColumnName()));
        });
        sqlBuilder.append(")").append(" values(").append(SqlBuildUtil.wrapSqlVal(String.valueOf(columnHolderList.get(0).getColumnVal())));
        columnHolderList.stream().skip(1).forEach(columnHolder -> {
            sqlBuilder.append(",").append(SqlBuildUtil.wrapSqlVal(String.valueOf(columnHolder.getColumnVal())));
        });
        sqlBuilder.append(");");
        String sql = sqlBuilder.toString();
        log.info("mock生成的SQL语句为: {}",sql);
        return sql;
    }
}
